package com.learn.project2.service.admin.auth;

import com.learn.project2.bean.*;
import com.learn.project2.config.shiro.MarketToken;
import com.learn.project2.mapper.MarketAdminMapper;
import com.learn.project2.mapper.MarketPermissionMapper;
import com.learn.project2.mapper.MarketRoleMapper;
import com.learn.project2.mapper.NewMarketRolePermissionGrandsonMapper;
import com.learn.project2.model.vo.admin.auth.AdminInfoVO;
import com.learn.project2.model.vo.admin.auth.AuthLoginVO;
import com.learn.project2.utils.AESUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @description: 登录
 * @author zhizhuke
 * @date 2022-01-10 19-32
 * @version 1.0
 */
@Service
public class  AuthServiceImpl implements AuthService{

    @Autowired
    MarketRoleMapper roleMapper;
    @Autowired
    MarketPermissionMapper permissionMapper;
    @Autowired
    NewMarketRolePermissionGrandsonMapper grandsonMapper;
    @Autowired
    MarketAdminMapper adminMapper;



    @Override
    public List<String> listRoles(MarketAdmin marketAdmin) {
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list,marketAdmin.getRoleIds());

        List<String> roles = new ArrayList<>();
        for (Integer roleId : list) {
            MarketRole role = roleMapper.selectByPrimaryKey(roleId);
            roles.add(role.getName());
        }
        return roles;
    }

    @Override
    public List<String> listPerms(MarketAdmin marketAdmin) {

        List<Integer> list = new ArrayList<>();
        Collections.addAll(list,marketAdmin.getRoleIds());
        MarketPermissionExample example = new MarketPermissionExample();
        example.createCriteria().andRoleIdIn(list);
        List<MarketPermission> marketPermissions = permissionMapper.selectByExample(example);
        List<String> permissions = new ArrayList<>();
        for (MarketPermission marketPermission : marketPermissions) {
            permissions.add(marketPermission.getPermission());
        }

        /**
         * @description: 遍历grand表
         * @param: [marketAdmin]
         * @return: java.util.List<java.lang.String>
         * @author 王力
         * @date: 2022-01-11 20:00:51
         */
        if (permissions.contains("*")) return permissions;
        List<String> perms = new ArrayList<>();
        for (String s : permissions) {
            NewMarketRolePermissionGrandsonExample grandsonExample = new NewMarketRolePermissionGrandsonExample();
            grandsonExample.createCriteria().andDetailNameEqualTo(s);
            List<NewMarketRolePermissionGrandson> grandsons = grandsonMapper.selectByExample(grandsonExample);
            for (NewMarketRolePermissionGrandson grandson : grandsons) {
                perms.add(grandson.getMethod()+grandson.getDetailApi());
            }
        }
        return perms;
    }

    @Override
    public AuthLoginVO authLogin(String username, String password, HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();

        String psw = AESUtil.encrypt(password, "niupia");


        subject.login(new MarketToken(username, psw,"admin"));

        MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipal();

        AdminInfoVO adminInfo = new AdminInfoVO();

        marketAdmin.setLastLoginTime(new Date());
        marketAdmin.setLastLoginIp(request.getRemoteHost());
        adminMapper.updateByPrimaryKeySelective(marketAdmin);


        adminInfo.setNickName(marketAdmin.getUsername());
        adminInfo.setAvatar(marketAdmin.getAvatar());

        AuthLoginVO authLoginVO = new AuthLoginVO();
        authLoginVO.setAdminInfo(adminInfo);
        authLoginVO.setToken(((String) subject.getSession().getId()));
        return authLoginVO;
    }


}
